package nicg.st.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import nicg.st.util.ErrorCode;
import nicg.st.util.ResponseUtil;

/**
 * 全局异常处理器
 *
 * @author GeChen
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
    	e.printStackTrace();
        if (e instanceof GlobalException) {
            GlobalException globalException = (GlobalException) e;
            return ResponseUtil.getResponseString(ErrorCode.ERROR, globalException.getCodeMsg(), "");
        }else if (e instanceof DbException) {
        	DbException dbException = (DbException) e;
        	return ResponseUtil.getResponseString(ErrorCode.DB_ERROR, dbException.getCodeMsg(), "");
        }
        else {
            return ResponseUtil.getResponseString(ErrorCode.ERROR, "服务端错误", "");
        }
    }
}
